/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.sistemas.sse.util;

import java.io.Serializable;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.TransactionException;
import org.hibernate.exception.ConstraintViolationException;

/**
 *
 * @author laptop-w8
 */
public class HibernateUtil {

    public static boolean agregarEntidad(SessionFactory sessionFactory, Object object) {
        boolean success = false;
        Session session = sessionFactory.openSession();

        try {
            session.beginTransaction();
            session.persist(object);
            session.flush();
            success = true;
        } catch (ConstraintViolationException he) {
            System.out.println("excepcion01: " + he);
            session.getTransaction().rollback();
        } catch (TransactionException he) {
            System.out.println("excepcion02: " + he);
            session.getTransaction().rollback();
        } catch (HibernateException he) {
            System.out.println("excepcion03: " + he);
            session.getTransaction().rollback();
        } finally {
            session.getTransaction().commit();
            session.close();
        }

        return success;
    }

    public static boolean actualizarEntidad(SessionFactory sessionFactory, Object object) {
        boolean success = false;
        Session session = sessionFactory.openSession();

        try {
            session.beginTransaction();
            session.update(object);
            session.flush();
            success = true;
        } catch (ConstraintViolationException he) {
            System.out.println("excepcion01: " + he);
            session.getTransaction().rollback();
        } catch (TransactionException he) {
            System.out.println("excepcion02: " + he);
            session.getTransaction().rollback();
        } catch (HibernateException he) {
            System.out.println("excepcion03: " + he);
            session.getTransaction().rollback();
        } finally {
            session.getTransaction().commit();
            session.close();
        }

        return success;
    }

    public static <T> T obtenerEntidad(SessionFactory sessionFactory, Class<T> claseEntidad, Serializable id) {

        T e = null;
        Session session = sessionFactory.openSession();

        try {
            session.getTransaction().begin();
            e = (T) session.get(claseEntidad, id);
        } catch (ConstraintViolationException he) {
            System.out.println("excepcion01: " + he);
            session.getTransaction().rollback();
        } catch (TransactionException he) {
            System.out.println("excepcion02: " + he);
            session.getTransaction().rollback();
        } catch (HibernateException he) {
            System.out.println("excepcion03: " + he);
            session.getTransaction().rollback();
        } finally {
            session.getTransaction().commit();
            session.close();
        }

        return e;
    }
}
